package com.xyy.saas.payment.job;

import com.alibaba.fastjson.JSON;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.xyy.elasticjob.client.annotation.ElasticJobConf;
import com.xyy.saas.payment.service.jd.JDBillService;
import com.xyy.saas.payment.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author wangzeqi
 */
@Slf4j
@ElasticJobConf(name = "com.xyy.saas.payment.job.JDPayBillCheckJob", cron = "0 30 21 * * ?")
public class JDPayBillCheckJob implements SimpleJob {

    @Value("${jDPayBillCheckJob.switch:true}")
    private Boolean jDPayBillCheckJobSwitch;
    @Value("${jDPayBillCheckJob.checkDate:}")
    private String checkDate;

    @Resource(name = "jDBillServiceImpl0026")
    private JDBillService jDBillServiceImpl0026;
    @Resource(name = "jDBillServiceImpl0028")
    private JDBillService jDBillServiceImpl0028;

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("JDPayBillCheckJob started...shardingContext = {}", JSON.toJSONString(shardingContext));

        if (!Boolean.TRUE.equals(jDPayBillCheckJobSwitch)) {
            log.info("JDPayBillCheckJob no execute, {}", jDPayBillCheckJobSwitch);
            return;
        }

        if (StringUtils.isBlank(checkDate)) {
            checkDate = DateUtil.format(new Date(), DateUtil.DATEFORMATDAY);
        }

        // 只check 0026、0028文件
        try {
            log.info("JDPayBillCheckJob checkBill0026 start, {}", checkDate);
            String msg = jDBillServiceImpl0026.checkBill(checkDate);
            log.info("JDPayBillCheckJob checkBill0026 end, {}-{}", checkDate, msg);
        } catch (Exception e) {
            log.error("JDPayBillCheckJob checkBill0026 error, {}-{}", checkDate, e.getMessage(), e);
        }

        try {
            log.info("JDPayBillCheckJob checkBill0028 start, {}", checkDate);
            String msg = jDBillServiceImpl0028.checkBill(checkDate);
            log.info("JDPayBillCheckJob checkBill0028 end, {}-{}", checkDate, msg);
        } catch (Exception e) {
            log.error("JDPayBillCheckJob checkBill0028 error, {}-{}", checkDate, e.getMessage(), e);
        }

    }
}
